/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.awt.Color;

/**
 *
 * @autho Benjamin
 */
public class Piece {

    public final static int NB_PIECES = 7; // Le nombre de types de pièces qu'on a
    public final static int NB_ROTATIONS = 4; // Le nombre de rotations de chaque pièce
    public final static int SIZE = 4; // La taille de la matrice de chaque rotation
    public final static int pieceOver[][] = {
        {1,1,1,1,0,0,1,1,1,1},
        {1,0,0,0,0,0,1,0,0,1},
        {1,0,0,1,0,0,1,0,0,1},
        {1,1,1,1,0,0,1,1,1,1},
        {0,0,0,0,0,0,0,0,0,0},
        {1,1,1,1,0,0,1,0,0,1},
        {1,0,0,1,0,0,1,0,0,1},
        {1,1,1,1,0,0,1,0,0,1},
        {1,0,0,1,0,0,0,1,1,0},
        {0,0,0,0,0,0,0,0,0,0},
        {1,1,1,1,0,0,1,1,1,1},
        {1,1,1,1,0,0,1,1,0,0},
        {1,0,0,1,0,0,1,1,0,0},
        {1,0,0,1,0,0,1,1,1,1},
        {0,0,0,0,0,0,0,0,0,0},
        {1,1,1,1,0,0,1,1,1,1},
        {1,1,0,0,0,0,1,0,0,1},
        {1,1,0,0,0,0,1,1,1,0},
        {1,1,1,1,0,0,1,0,0,1},
        {0,0,0,0,0,0,0,0,0,0}
    };
    public final static int[][][][] PIECES = // 1 = case occupée 2 = centre de rotation 0 = case non occupée      
            {
        { // O
            {
                {0, 0, 0, 0},
                {0, 1, 2, 0},
                {0, 1, 1, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 0, 0},
                {0, 1, 2, 0},
                {0, 1, 1, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 0, 0},
                {0, 1, 2, 0},
                {0, 1, 1, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 0, 0},
                {0, 1, 2, 0},
                {0, 1, 1, 0},
                {0, 0, 0, 0}
            }
        },
        { // I
            {
                {0, 0, 0, 0},
                {0, 0, 0, 0},
                {1, 1, 2, 1},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 1, 0},
                {0, 0, 1, 0},
                {0, 0, 2, 0},
                {0, 0, 1, 0}
            },
            {
                {0, 0, 0, 0},
                {0, 0, 0, 0},
                {1, 1, 2, 1},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 1, 0},
                {0, 0, 1, 0},
                {0, 0, 2, 0},
                {0, 0, 1, 0}
            }
        },
        { // S
            {
                {0, 0, 0, 0},
                {0, 0, 2, 1},
                {0, 1, 1, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 1, 0},
                {0, 0, 2, 1},
                {0, 0, 0, 1},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 0, 0},
                {0, 0, 2, 1},
                {0, 1, 1, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 1, 0},
                {0, 0, 2, 1},
                {0, 0, 0, 1},
                {0, 0, 0, 0}
            }
        },
        { // Z
            {
                {0, 0, 0, 0},
                {0, 1, 2, 0},
                {0, 0, 1, 1},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 0, 1},
                {0, 0, 2, 1},
                {0, 0, 1, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 0, 0},
                {0, 1, 2, 0},
                {0, 0, 1, 1},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 0, 1},
                {0, 0, 2, 1},
                {0, 0, 1, 0},
                {0, 0, 0, 0}
            }
        },
        { // L
            {
                {0, 0, 0, 0},
                {0, 1, 2, 1},
                {0, 1, 0, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 1, 0},
                {0, 0, 2, 0},
                {0, 0, 1, 1},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 0, 1},
                {0, 1, 2, 1},
                {0, 0, 0, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 1, 1, 0},
                {0, 0, 2, 0},
                {0, 0, 1, 0},
                {0, 0, 0, 0}
            }
        },
        { // J
            {
                {0, 0, 0, 0},
                {0, 1, 2, 1},
                {0, 0, 0, 1},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 1, 1},
                {0, 0, 2, 0},
                {0, 0, 1, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 1, 0, 0},
                {0, 1, 2, 1},
                {0, 0, 0, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 1, 0},
                {0, 0, 2, 0},
                {0, 1, 1, 0},
                {0, 0, 0, 0}
            }
        },
        { // T
            {
                {0, 0, 0, 0},
                {0, 1, 2, 1},
                {0, 0, 1, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 1, 0},
                {0, 0, 2, 1},
                {0, 0, 1, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 1, 0},
                {0, 1, 2, 1},
                {0, 0, 0, 0},
                {0, 0, 0, 0}
            },
            {
                {0, 0, 1, 0},
                {0, 1, 2, 0},
                {0, 0, 1, 0},
                {0, 0, 0, 0}
            }
        }
    };
    
    public enum TypePiece{
        O, I, S, Z, L, J,T;
    }
   
    /* Attribut d'une pièce */
    private TypePiece typePiece; 
    private int rotation; 
    private int offset_i; // Position sur la ligne
    private int offset_j; // Position sur la colonne
        
    public Piece(TypePiece type){
        typePiece = type;
        rotation = 0; //position par défaut
        offset_i = -2; //positions dans la grille
        offset_j = 3;
    }
    
    public int getRotation(){
        return rotation;
    }
    
    
    public void setRotation(int r){        
        if(r == 4){
            r = 0;
        }else if(r == -1){
            r = 3;
        }
        this.rotation = r;
    }
    
    public TypePiece getTypePiece(){
        return typePiece;
    }
    
    public void setTypePiece(TypePiece type){
        this.typePiece = type;
    }
    
    public int getOffset_i(){
        return this.offset_i;
    }
    
    public int getOffset_j(){
        return this.offset_j;
    }

    public void setOffset_i(int i){
        this.offset_i = i;
    }
    
    public void setOffset_j(int j){
        this.offset_j = j;
    }
    
    public final static Case[][] getTabOver(){
        Case[][] tab = new Case [20][10];
        Case c = null;
        for(int i = 0 ; i<20; i++){
            for(int j = 0 ; j<10; j++){
                if (pieceOver[i][j] == 1){
                    c = new Case(Color.DARK_GRAY, true);
                }
                else c = new Case(Color.WHITE, false);
            }
        }
        return tab;
    }
    
    public final Case[][] getTabPiece() {
        Case[][] tabC = new Case[4][4];
        int a = 0;
        Case c = null;
        
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {              
                if (this.typePiece.equals(TypePiece.O)){
                    a=0;
                    c = new Case(Color.PINK,true);
                }
                if (this.typePiece.equals(TypePiece.I)){
                    a=1;
                    c = new Case(Color.CYAN,true);
                }
                if (this.typePiece.equals(TypePiece.S)){
                    a=2;
                    c = new Case(Color.YELLOW,true);
                }
                if (this.typePiece.equals(TypePiece.Z)){
                    a=3;
                    c = new Case(Color.GREEN,true);
                }
                if (this.typePiece.equals(TypePiece.L)){
                    a=4;
                    c = new Case(Color.RED,true);
                }
                if (this.typePiece.equals(TypePiece.J)){
                    a=5;
                    c = new Case(Color.ORANGE,true);
                }
                if (this.typePiece.equals(TypePiece.T)){
                    a=6;
                    c = new Case(Color.DARK_GRAY,true);
                }
     
                if(PIECES[a][rotation][i][j] == 1 || PIECES[a][rotation][i][j] == 2){
                    tabC[i][j] = c;
                }else if(PIECES[a][rotation][i][j] == 0){
                    c = new Case(Color.WHITE, false);
                    tabC[i][j] = c;
                }
   
            }
        }
        return tabC;    
    }
    

    public void afficherPiece() {
        String str = "";
        int a = 0;
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                if (a != i) {
                    str += "\n";
                }
                a = i;
                str += PIECES[this.typePiece.ordinal()][this.rotation][i][j] + " ";
            }
        }
        System.out.print(str);
    }
}
